var request = require('request');

//请求数据
class HttpHelper {
    constructor() {
    };
    async post(url, data, isForm, headers) {
        return new Promise((resolve, reject) => {
            var option = {
                url: url,
                method: 'POST'
            };
            if (isForm)
                option.form = data;
            else {
                option.json = true;
                option.body = data;
            }
            if (headers) {
                option.headers = {};
                for (var key in headers)
                    option.headers[key] = headers[key];
            }
            request(option, (error, response, body) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve({ status: response.statusCode, data: body });
                }
            });
        });
    };

    async get(url, headers) {
        return new Promise((resolve, reject) => {
            var option = {
                url: url,
                method: 'GET',
            };
            if (headers) {
                option.headers = {};
                for (var key in headers)
                    option.headers[key] = headers[key];
            }
            request(option, (error, response, body) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve({ status: response.statusCode, data: body });
                }
            });
        });
    };
};
var httpHelper = new HttpHelper();
module.exports = httpHelper;